<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!-- start: Content -->
<div id="content" class="span10">

	<div class="row-fluid sortable">
		<div class="box span12">
			<div class="box-header" data-original-title>
				<h2>
					<i class="halflings-icon edit"></i><span class="break"></span>请添加新菜品
				</h2>
				<div class="box-icon">
					<a href="#" class="btn-setting"><i
						class="halflings-icon wrench"></i></a> <a href="#"
						class="btn-minimize"><i class="halflings-icon chevron-up"></i></a>
					<a href="#" class="btn-close"><i class="halflings-icon remove"></i></a>
				</div>
			</div>
			<div class="box-content">
				<form class="form-horizontal">
					<fieldset>
						<div class="control-group">
							<label class="control-label" for="focusedInput">菜品名称</label>
							<div class="controls">
								<input class="input-xlarge focused" id="dishName" type="text"
									value="${dish.dishName}">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label">菜品描述</label>
							<div class="controls">
								<textarea id="dishDesc" class="span6 m-wrap" rows="3">${dish.dishDesc}</textarea>
							</div>
						</div>

						<div class="control-group">
							<label class="control-label" for="selectError3">分类</label>
							<div class="controls">
								<select id="dishGroup">
									<c:forEach items="${dishgroup}" var="list" varStatus="status">
										<option value="${list.dicValue}">${list.dicValue}</option>
									</c:forEach>
								</select>
							</div>
						</div>
						<div class="control-group">
							<label class="control-label" for="focusedInput">价格</label>
							<div class="controls">
								<input id="dishPrice" class="input-xlarge focused" type="text"
									value="${dish.dishPrice}">
							</div>
						</div>
						<div class="control-group">
							<label class="control-label">上传图片</label>
							<div class="controls">
								<div class="fileupload fileupload-new"
									data-provides="fileupload">
									<input type="hidden">
									<div class="fileupload-new thumbnail"
										style="width: 200px; height: 150px;">
										<img src="${dish.imageMain}" alt="" id="imagepreview"
											style="width: 200px; height: 150px;" />
									</div>
									<div class="fileupload-preview fileupload-exists thumbnail"
										style="max-width: 200px; max-height: 150px; line-height: 20px;"></div>
									<div>
										<span class="btn btn-file"><span class="fileupload-new">选择图片</span>
											<input type="file" class="default" id="fileToUpload"
											name="file"></span>
										<div class="btn btn-primary" id="fileuploadbtn"
											onClick="uploadImage()">走你！</div>
									</div>
								</div>
							</div>
						</div>
						<div class="form-actions">
							<div class="btn btn-primary" id="submitbtn" onClick="saveDish()">保存</div>
							<div class="btn" id="cancelbtn">取消</div>
						</div>

					</fieldset>
					<input type="hidden" value="" id="dishImage" />
				</form>

			</div>
		</div>
		<!--/span-->

	</div>
	<!--/row-->


	<script>
		function saveDish() {

			var isvalid = isValidInput();
			if (isvalid == "0") {
				return false;
			}
			var url = "/houtai/dish/save.do";
			var dishgroup = $("#dishGroup option:selected").val();
			if (dishgroup == "其他") {

				return;
			}
			var param = new Object();
			param.dishName = $("#dishName").val();
			param.dishPrice = $("#dishPrice").val();
			param.dishDesc = $("#dishDesc").val();
			param.dishGroup = $("#dishGroup option:selected").val();
			param.imageMain = $("#dishImage").val();

			param.status = status;

			var oo = {};
			oo.url = url;
			oo.params = JSON.stringify(param);
			oo.success = function(data) {
				if (data.responsecode == "1") {
					alert(data.message);
					window.location.href = "/houtai/dish/list.do";
				} else {
					alert(data.message);
				}

			};
			oo.error = function() {
				alert("系统错误，更新失败");
			}
			sendRequest(oo);
		}

		function isValidInput() {
			var dishName = $("#dishName").val();
			var dishPrice = $("#dishPrice").val();
			var dishDesc = $("#dishDesc").val();
			var dishGroup = $("#dishGroup").val();
			var dishImage = $("#dishImage").val();
			var result = "1";
			if (dishName == "" || dishName == "undefined") {
				alert("请填写菜品名称");
				result = "0";
				$("#dishName").focus();
				// $("#dishName").css({"background-color":"yellow"});
				return result;
			}
			if (dishPrice == "" || dishPrice == "undefined") {
				alert("请填写菜品价格");
				result = "0";
				$("#dishPrice").focus();
				return result;
			} else {
				if (!/^\d{2}$/.test(dishPrice)) {
					alert("请填写有效价格");
					result = "0";
					$("#dishPrice").focus();
					return result;
				}
			}
			if (dishDesc == "" || dishDesc == "undefined") {
				alert("请填写描述信息");
				result = "0";
				$("#dishDesc").focus();
				return result;
			}
			if (dishGroup == "" || dishGroup == "undefined") {
				alert("请选择分组");
				result = "0";
				return result;
			}

			if (dishImage == "" || dishImage == "undefined") {
				alert("请选择图片");
				result = "0";
				return result;
			}

			return result;
		}

		//选择文件之后执行上传  

		function uploadImage() {
			var filename = $("#fileToUpload").val();
			if (filename == "") {
				alert("情先选择图片");
				return;
			}
			$.ajaxFileUpload({
				url : '/imageupload.do',
				secureuri : false,
				fileElementId : 'fileToUpload',//file标签的id  
				dataType : 'json',//返回数据的类型  
				data : {
					name : 'logan'
				},//一同上传的数据  
				success : function(data, status) {
					//把图片替换  
					alert(data.data);
					$("#dishImage").attr("value", data.data);
					$("#imagepreview").attr("src", data.data);

					if (typeof (data.error) != 'undefined') {
						if (data.error != '') {
							alert(data.error);
						} else {
							alert(data.msg);
						}
					}
				},
				error : function(data, status, e) {
					alert(e);
				}
			});
		};
	</script>

</div>
